#!/usr/bin/env python
#encoding=utf-8
import sys
sys.path.append('../')
from cutWord import cutWord
import jieba
import cPickle
from db_controlter import DB_controlter
import MySQLdb
import math

class Index_DB_controlter(DB_controlter):
    def __init__(self):
        DB_controlter.__init__(self)
        self.cursor.execute("CREATE TABLE IF NOT EXISTS \
            inversion(  id int unsigned not null auto_increment primary key, \
                        word varchar(32) not null, \
                        hot bigint(20) not null, \
                        posting_list longtext not null)")

    def has_this_word(self, word):
        self.cursor.execute(
            "SELECT id FROM inversion WHERE word = '%s' limit 1" %word)
        if self.cursor.fetchall():
            return True
        else:
            return False

    def insert_to_word_dictionary(self, word, _dict):
        hot = 0
        for each in _dict:
            hot += _dict[each]

        p = cPickle.dumps(_dict)
        self.cursor.execute(
            """INSERT INTO inversion (word, hot, posting_list) VALUES ('%s','%d','%s')""" %(word, hot, p.decode("utf8")))
        #self.db.commit()

    def get_posting_list(self, word):
        self.cursor.execute(
            "SELECT posting_list FROM inversion WHERE word = '%s' " %word)
        pl = self.cursor.fetchall()
        if pl:
            return cPickle.loads(pl[0][0].encode("utf8"))
        else:
            return dict()

    def get_hot(self, word):
        self.cursor.execute(
            "SELECT hot FROM inversion WHERE word = '%s' " %word)
        result = self.cursor.fetchall()
        if result:
            return result[0][0]
        else:     
            return 0

    def update_posting_list(self, word, hot, posting_list):
        self.cursor.execute("SELECT hot FROM inversion WHERE word = '%s' " %word)
        hot += self.cursor.fetchall()[0][0] 
        p = cPickle.dumps(posting_list)
        self.cursor.execute(
            "UPDATE inversion SET posting_list = '%s', hot = %d WHERE word = '%s' " %(p, hot, word))

    def get_latest_webpage_id(self):
        self.cursor.execute("SELECT id FROM WebPage_webpage ORDER BY id DESC LIMIT 1")
        return self.cursor.fetchall()[0]

class Inversion_Index(Index_DB_controlter):
    
    def __init__(self):
        Index_DB_controlter.__init__(self)

    def create_word_dictionary(self, content):
        webpage_id = self.get_latest_webpage_id()
        word_dict = cutWord(content)
        for word in word_dict:
            if self.has_this_word(word):
                posting_list = self.get_posting_list(word)
                posting_list[webpage_id] = word_dict[word]
                self.update_posting_list(word, word_dict[word], posting_list) 
            else:
                posting_list = dict()
                posting_list[webpage_id] = word_dict[word]
                self.insert_to_word_dictionary(word, posting_list)    

class HotWord(Index_DB_controlter):

    def __init__(self, content, n):
        Index_DB_controlter.__init__(self)

        self.hot_word_list = self._hot_words(content, n)
        self.hot_word_num = len(self.hot_word_list)
        print "hot_word_num : " + str(self.hot_word_num)

    def _hot_words(self, content, n):
        word_dict = cutWord(content)
        word_dict_len = len(word_dict)

        word_rank = dict()
        for word in word_dict:
            rank = word_dict[word] / math.log(1.1 + self.get_hot(word))
            word_rank[word] = rank

        word_rank = sorted(word_rank.items(), key = lambda item : item[1], reverse = True)
        #print word_rank
        #print len(word_rank)
        
        hot_word_list = list()

        if word_dict_len < n:
                n = word_dict_len
        
        for i in range(0, n):
            #print word_rank[i]
            hot_word_list.append(word_rank[i][0])

        return hot_word_list

    def get_hot_words_num(self):
        return self.hot_word_num

    def get_hot_words_str(self):
        return cPickle.dumps(self.hot_word_list)

    def get_hot_words_list(self, _str):
        return cPickle.loads(_str)

if __name__ == '__main__':
    xapain_index = Xapian()
    
    while True:
        word = raw_input('输入关键词:')

        # word是str类型,而索引中的键值是utf8类型的unicode
        # 因此需要将str转为unicode,用decode函数
        search_word = word.decode("utf8")
        for each in xapian_index.search(search_word):
            print each
